package com.example.service.impl.carecommunity.homebasedcare;

import com.example.bean.carecommunity.SheQujujia;
import com.example.bean.carecommunity.Vo.RiZhaoVO;
import com.example.mapper.carecommunity.homebasedcare.JujiayanglaoMapper;
import com.example.service.carecommunity.homebasedcare.JujiayanglaoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JujiayanglaoServiceImpl implements JujiayanglaoService {
    @Autowired
    JujiayanglaoMapper mapper;

    @Override
    public PageInfo<SheQujujia> selectJujiayanglaoAll(RiZhaoVO riZhaoVO) {
        Integer cp = riZhaoVO.getCurrentPage()==null?1:riZhaoVO.getCurrentPage();
        Integer ps = riZhaoVO.getPageSize()==null?10:riZhaoVO.getPageSize();
        PageHelper.startPage(cp,ps);
        PageInfo<SheQujujia> pageinfo = new PageInfo<SheQujujia>(mapper.selectJujiayanglaoAll(riZhaoVO));
        return pageinfo;
    }

    @Override
    public Integer addjujia(SheQujujia rijianzhaoliao) {
        //根据机构id查询出这个居家的特困人员供养机构类型和机构性质的信息后添加到主表中
        SheQujujia rz = mapper.selectJiGou(rijianzhaoliao.getJigouId());
        System.err.println("查到的机构信息："+rz);
        //特困人员供养机构类型
        rijianzhaoliao.setExtremelyNeedySupportType(rz.getExtremelyNeedySupportType());
        //机构性质
        rijianzhaoliao.setInstitutionsNature(rz.getInstitutionsNature());
        //往数据库中进行添加操作
        System.err.println("添加居家养老服务的信息："+rijianzhaoliao);
        Integer a = mapper.addjujia(rijianzhaoliao);
        //原因判断
        String xuke = rijianzhaoliao.getXuke();
        System.err.println("许可信息："+xuke);
        System.out.println("原因信息："+rijianzhaoliao.getYuanyin());
        //获取到刚添加的这条数据的id信息
        String noid = rijianzhaoliao.getNoid();
        System.err.println("获取到刚添加信息的id："+noid);
        //如果有许可证，则原因不用显示，填0即可,默认也不让其显示
        mapper.updateXukeBasc(noid);
        if(xuke.equals("0")){//没有许可证，显示原因，进行添加
            for(String i : rijianzhaoliao.getYuanyin()){//获取到没有许可证的原因
                if(i.equals("0")){
                    mapper.updatea(noid);
                }
                if(i.equals("1")){
                    mapper.updateb(noid);
                }
                if(i.equals("2")){
                    mapper.updatec(noid);
                }
                if(i.equals("3")){
                    mapper.updated(noid);
                }
            }
        }
        String fuwuxiangid = rijianzhaoliao.getFuwuxiangid();
        System.err.println("服务项的id："+fuwuxiangid);
        System.err.println(""+rijianzhaoliao.getFwgnlist());
        //往中间表中添加服务信息
        for(String index:rijianzhaoliao.getFwgnlist()) {
            mapper.addzhongjian(index, fuwuxiangid, noid);
        }
        return a;
    }
}